package com.neusoft.nepa.controller;

import com.neusoft.nepa.entity.User;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

public class AdminRegisterViewController {
    public void cancelRegister(ActionEvent event)throws IOException
    {
        Stage stage = (Stage) ((javafx.scene.Node) event.getSource()).getScene().getWindow();

        // 加载注册界面的FXML
        FXMLLoader loader = new FXMLLoader(getClass().getResource("/com/neusoft/nepa/view/AdminLoginView.fxml"));
        Parent fxmlContent = loader.load();

        // 创建背景容器，并将FXML内容添加为子节点
        BackgroundPane backgroundPane = new BackgroundPane();
        backgroundPane.getChildren().add(fxmlContent);
        fxmlContent.setStyle("-fx-background-color: transparent;"); // 确保FXML内容透明

        // 创建新场景，根容器为BackgroundPane
        Scene newScene = new Scene(backgroundPane, 960, 540); // 设置固定尺寸或自适应
        stage.setScene(newScene);
        stage.setTitle("Login");
        stage.show();
    }
    public void registerDoneEvent(ActionEvent event) throws IOException {
        System.out.println("registered");
        System.out.println("User name:"+userName.getText());
        System.out.println("Phone number:"+phoneNumber.getText());
        System.out.println("Passcode:"+passcode.getText());
        System.out.println("Confirm:"+confirmPasscode.getText());
        if(!phoneNumber.getText().isEmpty()) {
            try {
                Long.parseLong(phoneNumber.getText());  // 尝试转换为Long，若成功则认为是手机号


                if (!passcode.getText().equals(confirmPasscode.getText())) {
                    Alert alert = new Alert(AlertType.ERROR);
                    alert.setTitle("错误");
                    alert.setHeaderText(null); // 可选：设置标题栏文本（可null）
                    alert.setContentText("密码不匹配"); // 核心提示内容
                    alert.showAndWait(); // 阻塞显示对话框
                } else if (passcode.getText().isEmpty() || userName.getText().isEmpty() || phoneNumber.getText().isEmpty()) {
                    Alert alert = new Alert(AlertType.WARNING);
                    alert.setTitle("错误");
                    alert.setHeaderText(null); // 可选：设置标题栏文本（可null）
                    alert.setContentText("内容不能为空"); // 核心提示内容
                    alert.showAndWait();
                } else {
                    User u1 = new User(phoneNumber.getText(), userName.getText(), passcode.getText(), "");
                    Alert alert = new Alert(AlertType.INFORMATION);
                    alert.setTitle("已注册");
                    alert.setHeaderText(null); // 可选：设置标题栏文本（可null）
                    alert.setContentText("已注册\n用户名：" + u1.getUserName() + "\n用户编码：" + u1.getPhoneNumber() + "\n密码：" + u1.getPasscode()); // 核心提示内容
                    alert.showAndWait(); // 阻塞显示对话框

                    try {
                        File file = new File("Admin.txt");
                        Map<String, User> userMap = new HashMap();
                        if (file.exists() && file.length() > 0) {
                            try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))) {
                                userMap = (HashMap<String, User>) ois.readObject();
                            }
                        } else {
                            userMap = new HashMap();
                        }
                        userMap.put(u1.getPhoneNumber(), u1);
                        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file))) {
                            oos.writeObject(userMap);
                        }

                    } catch (IOException | ClassNotFoundException e) {
                        alert.setTitle("注册失败");
                        alert.setContentText("数据存储失败：" + e.getMessage());
                        alert.showAndWait();
                        e.printStackTrace();
                    }


                    Stage stage = (Stage) ((javafx.scene.Node) event.getSource()).getScene().getWindow();

                    // 加载注册界面的FXML
                    FXMLLoader loader = new FXMLLoader(getClass().getResource("/com/neusoft/nepa/view/AdminLoginView.fxml"));
                    Parent fxmlContent = loader.load();

                    // 创建背景容器，并将FXML内容添加为子节点
                    BackgroundPane backgroundPane = new BackgroundPane();
                    backgroundPane.getChildren().add(fxmlContent);
                    fxmlContent.setStyle("-fx-background-color: transparent;"); // 确保FXML内容透明

                    // 创建新场景，根容器为BackgroundPane
                    Scene newScene = new Scene(backgroundPane, 960, 540); // 设置固定尺寸或自适应
                    stage.setScene(newScene);
                    stage.setTitle("Login");
                    stage.show();


                }
            } catch (NumberFormatException e) {
                // 输入不是数字，按用户名处理
                Alert alert = new Alert(AlertType.ERROR);
                alert.setTitle("错误");
                alert.setHeaderText(null); // 可选：设置标题栏文本（可null）
                alert.setContentText("编码必须为数字"); // 核心提示内容
                alert.showAndWait();
            }
        }
    }
    @FXML
    private TextField userName;
    @FXML
    private TextField phoneNumber;
    @FXML
    private TextField passcode;
    @FXML
    private TextField confirmPasscode;
}